//
//  VideoDetailTableViewCell.m
//  Product_NEWS
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 FCH. All rights reserved.
//

#import "VideoDetailTableViewCell.h"
#import "UIImageView+WebCache.h"
@interface VideoDetailTableViewCell ()
@property (nonatomic, retain) UIImageView *headImageView;
@property (nonatomic, retain) titleLabel *replayLabel;
@property (nonatomic, retain) WordTite *timeLabel;
@property (nonatomic, retain) titleLabel *nameLabel;
@property(nonatomic,retain)UIImageView *textBackground;
@end


@implementation VideoDetailTableViewCell

- (void)dealloc{
    [_headImageView release];
    [_nameLabel release];
    [_timeLabel release];
    [_replayLabel release];
    [_model release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return  self;
}


- (void)createSubViews{
    
    // 文字背景图片
    self.textBackground = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, [[UIScreen mainScreen] bounds].size.width , 65 * SCREEN_HEIGHT)];
    
    [self.textBackground.layer setBorderWidth:1.0 * SCREEN_WIDTH];
    // 设置边框颜色
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref = CGColorCreate(colorSpace,(CGFloat[]){ 230/255.0, 230/255.0, 230/255.0, 1 }); // 灰色
    [self.textBackground.layer setBorderColor:colorref];
    self.textBackground.backgroundColor = [NightModeHandle shareNightModeHandle].allViewColor;
    [self addSubview:self.textBackground];
    
    // 用户头像
    self.headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20 * SCREEN_WIDTH, 10 * SCREEN_HEIGHT, 50* SCREEN_WIDTH, 50* SCREEN_HEIGHT)];
    // 设置图片圆角
    // 1.设置图片边框为隐藏状态
    self.headImageView.layer.masksToBounds = YES;
    // 2.设置图片圆角
    self.headImageView.layer.cornerRadius = 25; // 为imageView大小的一半即为圆
    [self addSubview:self.headImageView];
    
    // 用户名
    self.nameLabel = [[titleLabel alloc] initWithFrame:CGRectMake(80 * SCREEN_WIDTH, self.headImageView.frame.origin.y, 200* SCREEN_WIDTH, 20 * SCREEN_HEIGHT)];
    
    self.nameLabel.font = [UIFont systemFontOfSize:13*SCREEN_WIDTH];
    
    [self addSubview:self.nameLabel];
    
    // 评论内容
    self.replayLabel = [[titleLabel alloc] initWithFrame:CGRectMake(80 * SCREEN_WIDTH, self.nameLabel.frame.origin.y + 30 * SCREEN_HEIGHT, self.frame.size.width - 20 * SCREEN_WIDTH, 40 * SCREEN_HEIGHT)];
    self.replayLabel.font = [UIFont systemFontOfSize:14*SCREEN_WIDTH];
    
    self.replayLabel.numberOfLines = 0;
    self.replayLabel.lineBreakMode = NSLineBreakByCharWrapping;
    [self addSubview:self.replayLabel];
}

    
+ (CGFloat)heightForText:(NSString *)text width:(CGFloat)width
{
    CGSize size = CGSizeMake(width, 3000);
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14.0 * SCREEN_WIDTH] forKey:NSFontAttributeName];
    CGRect frame = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    return frame.size.height;
}

- (void)setModel:(VideoDetailModel *)model
{
    NSLog(@"执行了");
    
    
    if ( _model!= model) {
        [_model release];
        _model = [model retain];
    }
    
    NSString *tempStr = model.f;
    NSArray *array = [tempStr componentsSeparatedByString:@";"]; 
    NSString *string = [NSString stringWithFormat:@"%@", [array lastObject]];
    self.replayLabel.text = model.b;
    self.timeLabel.text = model.t;
    self.nameLabel.text = string;
    NSURL *url = [NSURL URLWithString:model.timg];
    [self.headImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"background"]];
    
    
    self.replayLabel.frame = CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height, [[UIScreen mainScreen] bounds].size.width - 80 - 20, [[self class] heightForText:model.b width:[[UIScreen mainScreen] bounds].size.width - 80 - 20]);

    // 背景
    if (self.replayLabel.frame.size.height > 40) {
        self.textBackground.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width - 20, 40 + self.replayLabel.frame.size.height + 5);
        
    }else{
        self.textBackground.frame = CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width - 20, 50 + 20);
    }

    
    
    
}





@end
